home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / ToyViewEvent.m < prev    next >
Encoding:
Text File  |  1995-11-12  |  5.6 KB  |  254 lines

  1. #import "ToyView.h"
  2. #import <dpsclient/wraps.h>
  3. #import <appkit/publicWraps.h>
  4. #import <appkit/NXImage.h>
  5. #import <appkit/NXBitmapImageRep.h>
  6. #import <appkit/Application.h>
  7. #import <appkit/NXCursor.h>
  8. #import <appkit/TextField.h>
  9. #import <stdlib.h>
  10. #import "PrefControl.h"
  11.  
  12. #define  CrossCursor    "cross.tiff"
  13.  
  14. static NXCursor *xCursor = nil;
  15. static BOOL originUpperLeft = NO;
  16.  
  17.  
  18. @implementation ToyView (EventHandling)
  19.  
  20. + cursor
  21. {
  22.     if (!xCursor) {
  23.     NXPoint spot;
  24.     xCursor = [NXCursor newFromImage:[NXImage newFromSection:CrossCursor]];
  25.     spot.x = 7.0; spot.y = 7.0;
  26.     [xCursor setHotSpot:&spot];
  27.     }
  28.     return xCursor;
  29. }
  30.  
  31. - (BOOL)acceptsFirstResponder
  32. {
  33.     return YES;
  34. }
  35.  
  36. - (BOOL)acceptsFirstMouse
  37. {
  38.     return YES;
  39. }
  40.  
  41. - resetCursorRects
  42. {
  43.     NXRect visible;
  44.  
  45.     if ([self getVisibleRect:&visible])
  46.         [self addCursorRect:&visible cursor:xCursor];
  47.     return self;
  48. }
  49.  
  50. /* Local Method */
  51. - drawDraggedLine: (NXCoord)x : (NXCoord)y : (int)dx : (int)dy
  52. {
  53.     char    buf[20], points[64];
  54.     int    y1, y2;
  55.  
  56.     if (originUpperLeft) {
  57.         y1 = curSize.height - y - dy;
  58.         y2 = curSize.height - y - 1;
  59.     }else {
  60.         y1 = y,  y2 = y + dy - 1;
  61.     }
  62.     sprintf(points, "%d x %d  (%d, %d : %d, %d)",
  63.             dx, dy, (int)x, y1, (int)x + dx - 1, y2);
  64.     [commText setStringValue: points];
  65.  
  66.     // already 'lockFocus'ed
  67.     PSnewinstance();
  68.     PSsetinstance(YES);
  69.     PSsetlinewidth(0.0);
  70.     PSsetgray(0.1667);
  71.     PSrectstroke(x, y+1, dx-1, dy-1);    /* Why +1 ?? */
  72.     if ( dx >= 18 && dy >= 10 ) {
  73.         sprintf(buf, "%d x %d", dx, dy);
  74.         PSmoveto(x+1, y+2);
  75.         PSgsave();
  76.         PSscale(1.0, -1.0);
  77.         PSshow(buf);
  78.         PSgrestore();
  79.     }
  80.     PSsetinstance(NO);
  81.     NXPing();
  82.     return self;
  83. }
  84.  
  85. - clearDraggedLine
  86. {
  87.     NXSize *sz = &selectRect.size;
  88.     if (sz->width > 0.0 || sz->height > 0.0) {
  89.         [self lockFocus];
  90.         PSnewinstance();
  91.         [self unlockFocus];
  92.         sz->width = 0.0;
  93.         sz->height = 0.0;
  94.         [commText setStringValue: comInfo->memo];
  95.     }
  96.     return self;
  97. }
  98.  
  99. - setDraggedLine: sender
  100. {
  101.     NXSize *sz = &selectRect.size;
  102.     if (sz->width > 0.0 && sz->height > 0.0) {
  103.         [self lockFocus];
  104.         [self drawDraggedLine: selectRect.origin.x
  105.             : selectRect.origin.y : sz->width : sz->height];
  106.         [self unlockFocus];
  107.     }
  108.     return self;
  109. }
  110.  
  111.  
  112. /* Code from pCD.app (H. Danisch & D. Phillips) */
  113.  
  114. #define DRAG_MASK (NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK)
  115. - mouseDown:(NXEvent *)event
  116. {
  117.     NXPoint    p, start, lowerleft;
  118.     BOOL    altDrag = NO, sftDrag = NO;
  119.     int    oldMask;
  120.     int    xn = 0, yn = 0, xs, ys;
  121.  
  122.     originUpperLeft = [preference originUpperLeft];
  123.     oldMask = [window addToEventMask:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  124.     p = event->location;
  125.     [self convertPoint:&p fromView:nil]; /* View based point */
  126.     if (p.x < 0.0) p.x = 0.0;
  127.     else if (p.x >= curSize.width) p.x = curSize.width - 1;
  128.     p.y--;
  129.     if (p.y < 0.0) p.y = 0.0;
  130.     else if (p.y > curSize.height) p.y = curSize.height - 1;
  131.     start.x = (int)p.x;
  132.     start.y = (int)p.y;
  133.     if (NX_ALTERNATEMASK &  event->flags)
  134.         altDrag = YES;
  135.  
  136.     [self lockFocus];
  137.     PSnewinstance();
  138.     PSsetinstance(YES);
  139.     event = [NXApp getNextEvent:DRAG_MASK];
  140.  
  141.     while( event->type != NX_MOUSEUP ) {
  142.         if (NX_ALTERNATEMASK &  event->flags)
  143.             altDrag = YES;
  144.         if (NX_SHIFTMASK &  event->flags)
  145.             sftDrag = YES;
  146.         p = event->location;
  147.         [self convertPoint:&p fromView:nil];
  148.         if (p.x < 0.0) p.x = 0.0;
  149.         else if (p.x >= curSize.width) p.x = curSize.width - 1;
  150.         p.y--;
  151.         if (p.y < 0.0) p.y = 0.0;
  152.         else if (p.y > curSize.height) p.y = curSize.height - 1;
  153.  
  154.         if (p.x > start.x)
  155.             xn = (int)p.x - start.x + 1,  xs = 1;
  156.         else
  157.             xn = start.x - (int)p.x + 1,  xs = -1;
  158.         if (p.y > start.y)
  159.             yn = (int)p.y - start.y + 1,  ys = 1;
  160.         else
  161.             yn = start.y - (int)p.y + 1,  ys = -1;
  162.         if (altDrag) {
  163.             if (xn < yn) yn = xn;
  164.             else xn = yn;
  165.         }
  166.         if (sftDrag) {
  167.             xn = (xn + 1) & ~3;
  168.             yn = (yn + 1) & ~3;
  169.         }
  170.         p.x = start.x + (xn-1)*xs;
  171.         p.y = start.y + (yn-1)*ys;
  172.         lowerleft.x = (start.x < p.x) ? start.x : p.x;
  173.         lowerleft.y = (start.y < p.y) ? start.y : p.y;
  174.         [self drawDraggedLine: lowerleft.x : lowerleft.y : xn : yn];
  175.         event = [NXApp getNextEvent:DRAG_MASK];
  176.     }
  177.     [self unlockFocus];
  178.         
  179.     selectRect.origin.x = lowerleft.x;
  180.     selectRect.origin.y = lowerleft.y;
  181.     selectRect.size.width  = xn;
  182.     selectRect.size.height = yn;
  183.     if (xn == 0 && yn == 0)        /* only click */
  184.         [commText setStringValue: comInfo->memo];
  185.     else if (xn < 3 && yn < 3)    /* Too small area */
  186.         [self clearDraggedLine];
  187.  
  188.     [window flushWindow];
  189.     [window setEventMask:oldMask];
  190.  
  191.     return self;
  192. }
  193.  
  194. - selectAll:sender
  195. {
  196.     [self clearDraggedLine];
  197.     selectRect.size = curSize;
  198.     selectRect.origin.x = selectRect.origin.y = 0.0;
  199.     [self setDraggedLine: sender];
  200.     return self;
  201. }
  202.  
  203. - copy:sender
  204. {
  205.     NXStream    *st;
  206.     NXBitmapImageRep *bm;
  207.     int        i = 0;
  208.     const char    *types[2];
  209.     id        pb = [Pasteboard new];
  210.  
  211.     if (selectRect.size.width < 1.0 || selectRect.size.height < 1.0) {
  212.         NXBeep();
  213.         return self;
  214.     }
  215.  
  216.     //  note that "owner:" in the following can not be "self", or the
  217.     //  id of anything else which might be freed inbetween "Copy"
  218.     //  operations.
  219.     types[i++] = NXTIFFPboardType;
  220.     [pb declareTypes:types num:i owner:NXApp];
  221.  
  222.     [self lockFocus];
  223.     bm = [[NXBitmapImageRep alloc] initData:NULL fromRect:&selectRect];
  224.     [self unlockFocus];
  225.  
  226.     st = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  227.     [bm writeTIFF:st];
  228.     [pb writeType:NXTIFFPboardType fromStream:st];
  229.     NXCloseMemory(st, NX_FREEBUFFER);
  230.     [bm free];
  231.  
  232.     return self;
  233. }
  234.  
  235. - drawSelf:(NXRect *)r :(int) count
  236. {
  237.     NXSize *sz;
  238.  
  239.     if (comInfo->alpha) {
  240.         PSsetgray(1.0);    
  241.         NXRectFill(r);
  242.         [image composite:NX_SOVER fromRect:r toPoint:&(r->origin)];
  243.     }else
  244.         [image composite:NX_COPY fromRect:r toPoint:&(r->origin)];
  245.  
  246.     sz = &selectRect.size;
  247.     if (sz->width > 0.0 && sz->height > 0.0)
  248.         [self perform:@selector(setDraggedLine:) with:self
  249.         afterDelay:30 cancelPrevious:YES];
  250.     return self;
  251. }
  252.  
  253. @end
  254.